package com.kui.trydoto.springmail;

import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

public class EmailUtil {

    public static MailSender getMailSender() {
        return getQQJavaMailSenderImpl();
    }

    public static JavaMailSenderImpl getQQJavaMailSenderImpl() {
        int port = 465;

        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        // Basic mail sender configuration, based on emailconfig.properties
        mailSender.setHost("smtp.qq.com");
        mailSender.setUsername(SelfProperties.mailUserName);
        mailSender.setPassword(SelfProperties.mailPassword);
        mailSender.setDefaultEncoding("UTF-8");

        // JavaMail-specific mail sender configuration, based on javamail.properties
        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        if (port == 587) {
            props.put("mail.smtp.starttls.enable", "true");
        }
        if (port == 465) { // 使用ssl 连接加密
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        }
        props.put("mail.debug", "true");
        mailSender.setJavaMailProperties(props);
        return mailSender;
    }
}
